package com.usooft.metaTalent.admin.system;

import com.usooft.framework.core.query.Page;
import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresPermission;
import com.usooft.framework.web.auth.RequiresPermissionDependOn;
import com.usooft.metaTalent.admin.system.form.SearchTermAddForm;
import com.usooft.metaTalent.admin.system.form.SearchTermEditForm;
import com.usooft.metaTalent.admin.system.form.SearchTermListForm;
import com.usooft.metaTalent.core.system.entity.SearchTerm;
import com.usooft.metaTalent.core.system.service.SearchTermService;
import com.usooft.metaTalent.core.system.wrapper.SearchTermWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author Xandy
 * @version 1.0.0
 * @description
 * @date 2023/10/25
 */
@RestController
@RequestMapping("/system/search-term")
@RequiredArgsConstructor
public class searchTermController {

    private final SearchTermService searchTermService;


    @GetMapping("/list")
    @RequiresPermission(value = "/system/search-term/list", description = "搜索词条/查询")
    public ResponseEntity<?> list(SearchTermListForm form) {
        Page<SearchTermWrapper> page = searchTermService.page(form.buildQuery());
        return ResponseEntity.ok(page);
    }

    @PostMapping("/create")
    @RequiresPermission(value = "/system/search-term/create", description = "搜索词条/添加")
    public ResponseEntity<?> create(SearchTermAddForm form) {
        searchTermService.create(form.getContent(), form.getIsHot(), form.getIsTop(), form.getPosition(), form.getPortType(), form.getSort());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/create/get")
    @RequiresPermissionDependOn("/system/search-term/create")
    public ResponseEntity<?> createGet() {
        return ResponseEntity.ok(MapBuilder.builder()
                .build());
    }

    @PostMapping("/edit")
    @RequiresPermission(value = "/system/search-term/edit", description = "搜索词条/修改")
    public ResponseEntity<?> edit(SearchTermEditForm form) {
        Optional<SearchTerm> optionalSearchTerm = searchTermService.find(form.getId());
        Assert.isTrue(optionalSearchTerm.isPresent(), "搜索词条不存在");
        searchTermService.edit(optionalSearchTerm.get(), form.getContent(), form.getIsHot(), form.getIsTop(), form.getPosition(), form.getPortType(), form.getSort());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/edit/get")
    @RequiresPermissionDependOn("/system/search-term/edit")
    public ResponseEntity<?> editGet(Long id) {
        SearchTerm searchTerm = searchTermService.get(id);
        return ResponseEntity.ok(MapBuilder.builder()
                .put("source", searchTerm)
                .build());
    }

}
